// <copyright file="InformationCardSTSServiceHostFactory.cs" company="SharpSTS">
// Copyright (c) 2007, 2008 All Right Reserved, http://sharpsts.com/
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Barry Dorrans</author>
// <email>barryd@idunno.org</email>
// <date>2008-06-14</date>
// <summary>Provides a custom service host for a security token service.</summary>

namespace SharpSTS.Web
{
    using System;
    using System.Security.Cryptography;
    using System.Security.Cryptography.X509Certificates;
    using System.ServiceModel;

    using Properties;
    using Configuration;

    /// <summary>
    /// A custom service host factory for the STS service hosts
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage(
        "Microsoft.Naming", 
        "CA1705:LongAcronymsShouldBePascalCased",
        Justification = "STS is not an acronym.")]
    internal sealed class InformationCardSTSServiceHostFactory : FixedHostNameServiceHostFactory
    {
        /// <summary>
        /// Gets the signing certificate.
        /// </summary>
        /// <value>The signing certificate.</value>
        public static X509Certificate2 SigningCertificate
        {
            get
            {
                string thumbprint = STSSettings.Settings.CertificateThumbprint;

                if (string.IsNullOrEmpty(thumbprint))
                {
                    throw new ArgumentException(Resources.ErrorMissingThumbprint);
                }

                X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection collection = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, true);
                
                if (collection.Count == 0)
                {
                    throw new CryptographicException(Resources.ErrorCertificateNotFound);
                }

                if (collection.Count > 1)
                {
                    throw new CryptographicException(Resources.ErrorMultipleCertificates);
                }

                return collection[0];
            }
        }

        /// <summary>
        /// Creates a <see cref="T:System.ServiceModel.ServiceHost"/> for a specified type of service with a specific base address and applies the 
        /// various required parameters to support Information Card methods to that ServiceHost.
        /// </summary>
        /// <param name="serviceType">Specifies the type of service to host.</param>
        /// <param name="baseAddresses">The <see cref="T:System.Array"/> of type <see cref="T:System.Uri"/> that contains the base addresses for the service hosted.</param>
        /// <returns>
        /// A <see cref="T:System.ServiceModel.ServiceHost"/> for the type of service specified with a specific base address.
        /// </returns>
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            baseAddresses = FixedHostNames(baseAddresses);
            ServiceHost serviceHost = new InformationCardSTSServiceHost(serviceType, baseAddresses, SigningCertificate);
            return serviceHost;
        }
    }
}
